package com.youu.youu.portal.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.atguigu.util.HttpClientUtil;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.youu.youu.manager.bean.TUser;
import com.youu.youu.portal.bean.ScwReturn;

@Controller
public class UserController {

	
	@Autowired
	RestApiServerInfo apiServerInfo;
	@ResponseBody
	@RequestMapping("/login")
	public Map<String,Object> login(@RequestParam("loginacct")String loginacct,@RequestParam("password")String password,@RequestParam("city")String city,@RequestParam("lng")String lng,@RequestParam("lat")String lat,HttpSession session) throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		System.out.println("进入portal登陆");

		Object attribute = session.getAttribute("tUser");
		System.out.println(attribute);
		if(attribute!=null){
			map.put("code", new Integer(3));
			map.put("LocationMsg", null);
			return map;
		}
		String url= apiServerInfo.getRestApiURL()+"/login";
		Map<String, Object> params = new HashMap<>();
		params.put("loginacct", loginacct);
        params.put("password", password);
        params.put("city", city);
        params.put("lng", lng);
        params.put("lat", lat);
		String request = HttpClientUtil.httpPostRequest(url, params);
		 ScwReturn<TUser> readValue = new ObjectMapper().readValue(request.getBytes(), new TypeReference<ScwReturn<TUser>>(){});
		 if(readValue.getCode()==1){
			 session.setAttribute("tUser", readValue.getContent());
			 System.out.println("登陆成功");
			 if(readValue.getExt()!=null){
				 map.put("code", new Integer(1));
				 map.put("LocationMsg", readValue.getExt().get("LocationMsg"));
				 return map;
			 }
			 map.put("code", new Integer(1));
			 map.put("LocationMsg", null);
			 return map;
		 }else{
			 map.put("code", new Integer(0));
			 map.put("LocationMsg", null);
			 return map;
		 }
	}
	//登出
	@RequestMapping("/logOut")
	public String logOut(HttpSession session){
		Object object = session.getAttribute("tUser");
		session.removeAttribute("tUser");
		System.out.println(object);
		return "redirect:/index.jsp";
	}
	
	//验证token
	@ResponseBody
	@RequestMapping("/toVerifyToken")
	public Integer toVerifyToken(HttpSession session) throws Exception{
		TUser attribute = (TUser)session.getAttribute("tUser");
		if(attribute==null){
			return 0;
		}
		String token = attribute.getToken();
		System.out.println("session中的token"+token);
		String url= apiServerInfo.getRestApiURL()+"/show/toVerifyToken";
		Map<String, Object> params = new HashMap<>();
		params.put("token", token);
		String request = HttpClientUtil.httpGetRequest(url, params);
		 ScwReturn<TUser> readValue = new ObjectMapper().readValue(request.getBytes(), new TypeReference<ScwReturn<TUser>>(){});
		if(readValue.getCode()==1){
			session.removeAttribute("error");
			System.out.println("状态正常继续操作");
			return 1;
		}else{
			session.removeAttribute("tUser");
			session.setAttribute("error",readValue.getMsg());
			return 0;
		}
	}
}
